package com.mldong.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mldong.auth.Perm;
import com.mldong.base.*;
import com.mldong.holder.LoginUserHolder;
import com.mldong.modules.sys.dto.UserPageParam;
import com.mldong.modules.sys.dto.UserParam;
import com.mldong.modules.sys.dto.UserRoleParam;
import com.mldong.modules.sys.service.RoleService;
import com.mldong.modules.sys.service.UserService;
import com.mldong.modules.sys.vo.LoginVO;
import com.mldong.modules.sys.vo.UserVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
    * 用户 前端控制器
    * </p>
*
* @author mldong
* @since 2022-10-19
*/
@RestController
@Api(tags = "用户管理")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final RoleService roleService;
    /**
     * 添加用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/save")
    @ApiOperation(value = "添加用户")
    @Perm
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) UserParam param) {
        userService.save(param);
        return CommonResult.ok();
    }
    /**
     * 删除用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/remove")
    @ApiOperation(value = "删除用户")
    @Perm
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        userService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 修改用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/update")
    @ApiOperation(value = "修改用户")
    @Perm
    public CommonResult<?> update(@RequestBody @Validated({Groups.Update.class}) UserParam param) {
        userService.update(param);
        return CommonResult.ok();
    }
    /**
     * 查询单个用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/detail")
    @ApiOperation(value = "查询单个用户")
    @Perm
    public CommonResult<UserVO> getById(@RequestBody @Validated IdParam param) {
        UserVO user = userService.findById(param.getId());
        return CommonResult.data(user);
    }
    /**
     *分页查询用户列表
     * @param param
     * @return
     */
    @PostMapping("/sys/user/page")
    @ApiOperation(value = "分页查询用户列表")
    @Perm
    //@SaCheckPermission(value = {"admin","sys:user:page"},mode = SaMode.OR)
    @SaCheckPermission("sys:user:page")
    public CommonResult<CommonPage<UserVO>> page(@RequestBody UserPageParam param) {
        return CommonResult.data(userService.page(param));
    }
    /**
     * 重置密码
     * @param param
     * @return
     */
    @PostMapping("/sys/user/resetPassword")
    @ApiOperation(value = "重置密码")
    @Perm
    public CommonResult<?> resetPassword(@RequestBody @Validated IdsParam param) {
        userService.resetPassword(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 个人-修改头像
     * @param param
     * @return
     */
    @PostMapping("/sys/user/updateAvatar")
    @ApiOperation(value = "个人-修改头像")
    public CommonResult<?> updateAvatar(@RequestBody @Validated({UserParam.ValidAvatar.class}) UserParam param) {
        userService.updateAvatar(param);
        return CommonResult.ok();
    }
    /**
     * 个人-修改用户信息
     * @param param
     * @return
     */
    @PostMapping("/sys/user/updateInfo")
    @ApiOperation(value = "个人-修改用户信息")
    public CommonResult<?> updateInfo(@RequestBody @Validated({UserParam.ValidInfo.class}) UserParam param) {
        userService.updateInfo(param);
        return CommonResult.ok();
    }
    /**
     * 个人-修改密码
     * @param param
     * @return
     */
    @PostMapping("/sys/user/updatePwd")
    @ApiOperation(value = "个人-修改密码")
    public CommonResult<?> updatePwd(@RequestBody @Validated({UserParam.ValidUpdatePwd.class}) UserParam param) {
        Long userId = LoginUserHolder.getUserId();
        String password = param.getPassword();
        String newPassword = param.getNewPassword();
        userService.updatePwd(userId, password, newPassword);
        return CommonResult.ok();
    }
    /**
     * 个人-用户信息
     * @return
     */
    @PostMapping("/sys/user/info")
    @ApiOperation(value = "个人-用户信息")
    public CommonResult<UserVO> info() {
        Long userId = LoginUserHolder.getUserId();
        UserVO user = userService.findById(userId);
        user.setPassword(null);
        user.setSalt(null);
        user.getExt().putAll(LoginUserHolder.get().getExt());
        LoginVO loginVO = LoginUserHolder.get();
        user.setLastLoginTime(loginVO.getLastLoginTime());
        if(ObjectUtil.isEmpty(user.getDeptName())){
            // 如果数据库中没有值，则从登录会放中获取部门信息
            user.setDeptName(loginVO.getDeptName());
        }
        if(ObjectUtil.isEmpty(user.getRoleNames())){
            // 如果数据库中没有值，则从登录会放中获取角色信息
            user.setRoleNames(loginVO.getRoleNameList());
        }

        return CommonResult.data(user);
    }
    /**
     * 个人-权限标识
     * @return
     */
    @PostMapping("/sys/user/permCode")
    @ApiOperation(value = "个人-权限标识")
    public CommonResult<List<String>> permCode() {
        List<String> permCodeList = LoginUserHolder.get().getPerms();
        return CommonResult.data(permCodeList);
    }
    /**
     * 授权角色
     * @param param
     * @return
     */
    @PostMapping("/sys/user/grantRole")
    @ApiOperation(value = "授权角色")
    @Perm
    public CommonResult<?> grantRole(@RequestBody @Validated({UserRoleParam.GrantRole.class}) UserRoleParam param) {
        userService.grantRole(param.getUserId(),param.getRoleIdList());
        return CommonResult.ok();
    }
    /**
     * 锁定用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/locked")
    @ApiOperation(value = "锁定用户")
    @Perm
    public CommonResult<?> locked(@RequestBody @Validated IdsParam param) {
        userService.locked(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 取消锁定用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/unLocked")
    @ApiOperation(value = "取消锁定用户")
    @Perm
    public CommonResult<?> unLocked(@RequestBody @Validated IdsParam param) {
        userService.unLocked(param.getIds());
        return CommonResult.ok();
    }
    /**
     *用户下拉列表
     * @param param
     * @return
     */
    @PostMapping("/sys/user/select")
    @ApiOperation(value = "用户下拉列表")
    public CommonResult<List<LabelValueVO>> select(@RequestBody UserPageParam param) {
        if(StrUtil.isEmpty(param.getLabelKey())){
            param.setLabelKey("realName");
        }
        if(StrUtil.isEmpty(param.getValueKey())){
            param.setValueKey("id");
        }
        return CommonResult.data(roleService.select("sys", "user", param));
    }
}
